//This clock steals timing relations from the famous pardusclock by Spoilerhead
//http://stud3.tuwien.ac.at/~e0326004/pardus/pardusclock/onlineclock/clock.html
//
//Spoilerheads script is licenced under GPL2, so I guess this is also ;)
//
// - Jirina
//

function POGetClock () {
    var c = top.po_clock;
    if (c == null || c == undefined) {
        c = top.po_clock = new POClock;
        setInterval("POClockTick()",1000);
        c.updateElements = new Array ();
    } 
    return c;
    //document.getElementById("my_tim").innerHTML = c.getTime();
    //document.getElementById("my_gmt").innerHTML = c.getGMT();
    //document.getElementById("my_ap").innerHTML = c.getAPS();
    //document.getElementById("my_bui").innerHTML = c.getBuildingTime();
    //document.getElementById("my_pl").innerHTML = c.getPLTime();
    //document.getElementById("my_sb").innerHTML = c.getStarbaseTime();
}
top.POGetClock = POGetClock;

function POClock(){
    this.time = new Date;
    this.pardus_time = this.getPardusTime();
}

function POClockTick () {
    c = POGetClock ();
    c.clockTick ();
}

POClock.prototype.updateElements = null;

POClock.prototype.createTimeElement = function (key, element, type, compact, update, realtime, prefix, postfix) {
    if (!update) {
        var reg = {};
        reg.element = element;
        reg.compact = compact==null?false:compact;
        reg.prefix = prefix==null?"":prefix;
        reg.postfix = postfix==null?"":postfix;
        this.tickElement (reg);   
        return element;
    }
    
    var reg = this.getElementByKey (key);
    if (!reg) {
        reg = {};
        reg.key = key;
        this.updateElements.push (reg);
    }

    reg.element = element;
    reg.type = type;
    reg.realtime = realtime;
    reg.compact = compact==null?false:compact;
    reg.prefix = prefix==null?"":prefix;
    reg.postfix = postfix==null?"":postfix;
    this.tickElement (reg);
    
    return element;
}

POClock.prototype.getElementByKey = function (key) {
    var els = this.updateElements;
    for (var i=0; i<els.length; i++) {
        var reg = els[i];
        if (reg.key == key) return reg;
    }
    return null;
}

POClock.prototype.tickElements = function (realtimeOnly) {
    var els = this.updateElements;
    for (var i=0; i<els.length; i++) {
        var reg = els[i];
        if (realtimeOnly && !reg.realtime) continue;
        this.tickElement (reg);
    }
}

POClock.prototype.tickElement = function (reg) {
    var options = {};
    options.compact = reg.compact;
    reg.element.textContent = reg.prefix + this.getTimeType(reg.type, options) + reg.postfix;
    
    if (options.coloring_off != true) {
        if (options.countdown == true) {
            reg.element.style.color = "red";
        } else if (options.resolution == "minute") {
            reg.element.style.color = "lightskyblue";
        } else if (options.resolution == "second") {
            reg.element.style.color = "orange";
        }
    }
}

POClock.prototype.getTimeType = function (type, options) {
    switch (type) {
        case "getTime": return this.getTime(options);
        case "getGMT": return this.getGMT(options);
        case "getAPS": return this.getAPS(options);
        case "getBuildingTime": return this.getBuildingTime(options);
        case "getPLTime": return this.getPLTime(options);
        case "getStarbaseTime": return this.getStarbaseTime(options);
        case "getNPCTime": return this.getNPCTime(options);
        case "getLeechTime": return this.getLeechTime(options);
        case "getACSTime": return this.getACSTime(options);
    }
    return type + " - undefined";
}

POClock.prototype.clockTick = function () {
    this.time = new Date;
    this.pardus_time = this.getPardusTime();
    this.tickElements (true);
}

POClock.prototype.prettyTime = function(h,m,s,options){
    var compact = false;
    if (options) {
        if (options.compact == true) compact = true;
    }
    
    //pad with zeroes if needed
    hours = (h > 9) ? "" + h : "0" + h;
    minutes = (m > 9) ? "" + m : "0" + m;
    seconds = (s > 9) ? "" + s : "0" + s;
    
    if (h != 0) {
        options.resolution = "hour";
    } else {
        if (m > 0) options.resolution = "minute";
        else options.resolution = "second";
    }
    
    if (compact) {
        if (h != 0) {
            return hours + ":" + minutes;
        } else {
            return minutes + ":" + seconds;
        }
    } else {
        return hours + ":" + minutes + ":" + seconds;
    }
}

POClock.prototype.getPardusTime = function(){
    // Return the pardus-time, which is 25 minutes late
    var my_time = new Date;
    var hours = my_time.getHours();
    var minutes = my_time.getMinutes();

    var min = minutes - 25;
    if( min < 0 )
    {
            min += 60;
            hours--;
    }

    if( hours < 0 )
    {
            hours = 23;
    }

    my_time.setMinutes(min);
    my_time.setHours(hours);

    return my_time;
}

POClock.prototype.getTime = function(options){
    var hours = this.time.getHours() % 24;
    var minutes = this.time.getMinutes();
    var seconds = this.time.getSeconds();

    options.coloring_off = true;
    
    return this.prettyTime(hours, minutes, seconds, options);
}

POClock.prototype.getGMT = function(options){
    var gmtHours = this.time.getTimezoneOffset()/60;
    var hours = (this.time.getHours()+gmtHours) % 24;
    var minutes = this.time.getMinutes();
    var seconds = this.time.getSeconds();

    options.coloring_off = true;
    
    return this.prettyTime(hours, minutes, seconds, options);
}

POClock.prototype.getAPS = function(options){
    var hours = 0;
    var minutes = 5 - this.time.getMinutes() % 6;
    var seconds = 59 - this.time.getSeconds();

    if (hours == 0 && minutes == 0) {
        options.countdown = true;
    } else {
        options.countdown = false;
    }
    
    return this.prettyTime(hours, minutes, seconds, options);
}

POClock.prototype.getBuildingTime = function(options){
    var gmtHours = this.pardus_time.getTimezoneOffset()/60;
    var hours = this.pardus_time.getHours();
    var minutes = 59 - this.pardus_time.getMinutes();
    var seconds = 59 - this.pardus_time.getSeconds();

    hours += (gmtHours + 5);
    hours = 5 - (hours % 6);

    if (hours == 0 && minutes < 30) {
        options.countdown = true;
    } else {
        options.countdown = false;
    }
    
    return this.prettyTime(hours, minutes, seconds, options);
}

POClock.prototype.getPLTime = function(options){
    var gmtHours = this.pardus_time.getTimezoneOffset()/60;
    var hours = this.pardus_time.getHours();
    var minutes = 59 - this.pardus_time.getMinutes();
    var seconds = 59 - this.pardus_time.getSeconds();

    hours += (gmtHours + 5);
    hours = 2 - ((hours + 2) % 3);

    if (hours == 0 && minutes < 30) {
        options.countdown = true;
    } else {
        options.countdown = false;
    }
    
    return this.prettyTime(hours, minutes, seconds, options);
}

POClock.prototype.getStarbaseTime = function(options){
    var gmtHours = this.pardus_time.getTimezoneOffset()/60;
    var hours = this.pardus_time.getHours();
    var minutes = 59 - this.pardus_time.getMinutes();
    var seconds = 59 - this.pardus_time.getSeconds();

    hours += (gmtHours + 5);
    hours = 2 - ((hours + 1) % 3);
    if (hours == 0 && minutes < 30) {
        options.countdown = true;
    } else {
        options.countdown = false;
    }
    return this.prettyTime(hours, minutes, seconds, options);
}

POClock.prototype.getStarbaseTimeFrom = function(time){
    var gmtHours = time.getTimezoneOffset()/60;
    var hours = time.getHours();
    var minutes = 59 - time.getMinutes();
    var seconds = 59 - time.getSeconds();

    hours += (gmtHours + 5);
    hours = 2 - ((hours + 1) % 3);

    time.setTime ((seconds*1000)+(minutes*60000)+(hours*3600000));
    return time;
}

POClock.prototype.getNPCTime = function(options){
    hours = 0;
    seconds = 59 - this.time.getSeconds();
    minutes = 8 - this.time.getMinutes() % 9;
    return this.prettyTime(hours, minutes, seconds, options);
}

POClock.prototype.getLeechTime = function(options){
    hours = 0;
    seconds = 59 - this.time.getSeconds();
    minutes = 19 - this.time.getMinutes() % 20;
    return this.prettyTime(hours, minutes, seconds, options);
}

POClock.prototype.getACSTime = function(options){
    hours = 0;
    seconds = 59 - this.time.getSeconds();
    minutes = 0;
    return this.prettyTime(hours, minutes, seconds, options);
}

POClock.prototype.getSBTicksSince = function(lastTime){
    var last = new Date ();
    last.setTime (lastTime);
    var nowms = this.pardus_time.getTime ();
    var lastTimeout = this.getStarbaseTimeFrom(last);
    var lastms = lastTimeout.getTime();

    // If the difference between Now and LastTime is smaller than what the time-to-tick was at last time,
    // we can safely say that there was no tick until last time.
    if ((nowms - lastTime) < lastms) return 0;
    else {
        // OK, now we simply count amount of whole 3h intervals between Now and LastTime
        var ticks = Math.floor((nowms - lastTime) / (3*3600000));
        return ticks;
    }
}

POGetClock ();